using System.Collections.Generic;
using System.Linq;
using UnityEngine.Networking;

namespace Client.Hotfix
{
    public static class CRequestFactory
    {
        public enum RequestType
        {
            Get,
            Post
        }

        private const int InitPoolCount = 10;
        private const int TimeOut = 3;

        static CRequestFactory()
        {
            for (int i = 0; i < InitPoolCount; ++i)
            {
                pool.Push(new UnityWebRequest());
            }
        }


        static private readonly Stack<UnityWebRequest> pool = new Stack<UnityWebRequest>();
        static public UnityWebRequest Pop(RequestType type = RequestType.Get)
        {
            UnityWebRequest request = new UnityWebRequest();
            request.method = UnityWebRequest.kHttpVerbGET;
            if (type == RequestType.Post)
            {
                request.method = UnityWebRequest.kHttpVerbPOST;
            }
            request.timeout = TimeOut;
            return request;
        }

        static public UnityWebRequest Pop(int timeOut, RequestType type = RequestType.Get)
        {
            UnityWebRequest request = new UnityWebRequest();
            request.method = UnityWebRequest.kHttpVerbGET;
            if (type == RequestType.Post)
            {
                request.method = UnityWebRequest.kHttpVerbPOST;
            }
            request.timeout = timeOut;
            return request;
        }

        static public void Push(UnityWebRequest request)
        {
            request.Dispose();
        }
    }

}
